import Vue from 'vue';
import VueRouter from 'vue-router';
import Layout from '@/views/Layout.vue';
import { getToken } from '@/utils/auth';
import routerView from '@/components/routerView.vue';

Vue.use(VueRouter);
//解决vue路由重复导航错误
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

// buffer:是否缓存  hidden:是否显示  isRouter:是否是导航路由
const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
    meta: { title: '登录-智慧财务管理系统' }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        component: () => import('@/views/index.vue'),
        meta: { title: '首页', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: false },
      },
      {
        path: '/user',
        name: 'user',
        component: () => import('@/views/user/index.vue'),
        meta: { title: '个人中心', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: false },

      },
      {
        path: '/goods',
        name: 'goods',
        component: routerView,
        meta: { title: '商品管理', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: true },
        children: [
          {
            path: '/goods/list',
            name: 'list',
            component: () => import('@/views/goods/list.vue'),
            meta: { title: '商品列表', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: false },
          },
          {
            path: '/goods/issue',
            name: 'issue',
            component: () => import('@/views/goods/issue.vue'),
            meta: { title: '商品发布', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: false },
          },
        ]
      }, 
      {
        path: '/system',
        name: 'system',
        component: routerView,
        meta: { title: '系统管理', icon: 'el-icon-setting', buffer: false, hidden: true, isRouter: true },
        children: [
          {
            path: '/system/menu',
            name: 'menu',
            component: () => import('@/views/system/menu.vue'),
            meta: { title: '菜单管理', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: true },
          },
          {
            path: '/system/role',
            name: 'role',
            component: () => import('@/views/system/role.vue'),
            meta: { title: '权限管理', icon: 'el-icon-location', buffer: false, hidden: true, isRouter: true },
          },
          {
            path: '/system/setting',
            name: 'setting',
            component: () => import('@/views/system/setting.vue'),
            meta: { title: '系统设置', icon: 'el-icon-setting', buffer: false, hidden: true, isRouter: true },
          },
        ]
      },

    ]
  },
];


const router = new VueRouter({
  mode: 'history',
  // base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path !== '/login') {
    if (!getToken()) {
      return next('/login');
    }
  }
  next();
  // 动态设置页面标题
  document.title = to.meta.title;
});
export default router;
